﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.UserControls.StructView;
using AdminModule.CommonClasses;
using AdminModule.IndexMarkServiceReference;

namespace AdminModule.Views
{
    public partial class SelectTargetCopyWindow : ChildWindow, IAddObjectSelector
    {
        private int IDTerm;
        private StructName structName;

        private DTOBase selectedObject = null;
        private StructViewBase structViewBase = new StructViewBase();

        public SelectTargetCopyWindow(StructName structName, int IDTerm)
        {
            InitializeComponent();
            this.structName = structName;
            this.IDTerm = IDTerm;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.selectedObject == null)
            {
                MessageBox.Show("Выберите узел, в который необходимо поместить копируемый элемент", MyAssemblyInfo.Title, MessageBoxButton.OK);
                return;
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            this.selectedObject = radioButton.Tag as DTOBase;
        }

        public DTOBase GetObject()
        {
            return this.selectedObject;
        }

        public IEnumerable<DTOBase> GetObjects()
        {
            throw new NotImplementedException();
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            switch (structName)
            {
                case StructName.IndexMark:
                    this.structViewBase = this.indexMarkStruct;
                    this.structViewBase.SetTemplate(Resources["SectionRootLevel"] as DataTemplate);
                    break;
                case StructName.University:
                    this.structViewBase = this.universityStruct;
                    this.structViewBase.SetTemplate(Resources["DepartmentRootLevel"] as DataTemplate);
                    break;
                case StructName.Gradations:
                    this.structViewBase = this.gradationsStruct;
                    this.structViewBase.SetTemplate(Resources["GragationsRootLevel"] as DataTemplate);
                    break;
            }

            structViewBase.ViewModel.InitStarted += () =>
                {
                    progressBar.IsVisibility = Visibility.Visible;
                };

            structViewBase.ViewModel.InitCompleted += () =>
                {
                    progressBar.IsVisibility = Visibility.Collapsed;
                };

            structViewBase.events.DataLoadStarted += () =>
                {
                    progressBar.IsVisibility = Visibility.Visible;
                };

            structViewBase.events.DataLoadCompleted += () =>
                {
                    progressBar.IsVisibility = Visibility.Collapsed;
                };

            this.structViewBase.Visibility = Visibility.Visible;
            this.structViewBase.Init(this.IDTerm);
            this.structViewBase.LoadData();
        }

        private void rootNode_Loaded(object sender, RoutedEventArgs e)
        {
            structViewBase.TreeView.ExpandToDepth(1);
        }
    }
}

